fix(sparse): floor() returns a copy instead of mutating its input (#740)#894
Merged
Merged
Conversation
…sact#740) chainladder.utils.sparse.floor(x) reassigned x.data in place, mutating the caller's COO array. np.floor() returns a copy, so floor() should too. Copy the array before flooring its data, mirroring the existing nan_to_num() idiom in the same module. The one internal caller (core/correlation.py) already uses the return value, so behavior is preserved. Updated the test that asserted in-place mutation to assert the new copy semantics: the result is a new object and the input is left unchanged. Closes casact#740.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #894 +/- ##
=======================================
Coverage 86.90% 86.90%
=======================================
Files 87 87
Lines 4932 4933 +1
Branches 624 624
=======================================
+ Hits 4286 4287 +1
Misses 456 456
Partials 190 190
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Collaborator
|
since we are already touching this function, may I please trouble you to add type hinting for the input and output? |
Per @henrydingliu's review on casact#894: annotate floor(x: COO) -> COO. COO is already imported at the module top (from sparse import COO as COO), so no new import is needed.
Contributor
Author
|
Added in |
henrydingliu
approved these changes
Jun 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #740.
chainladder.utils.sparse.floor(x)reassignedx.datain place, mutating the caller's COO array as a side effect:Since
np.floor()returns a copy, this makesfloor()match that contract by copying before flooring, using the same.copy()idiom already present innan_to_num()in the same module:Behavior now matches the issue's expectation:
The only internal caller (
chainladder/core/correlation.py) already consumes the return value (m = xp.floor((n - 1) / 2)), so the change is behavior-preserving there.Updated
test_floor_mutates_in_place(which assertedresult is a) totest_floor_returns_copy, asserting the result is a new object and the input is left unmodified.test_sparse.pyandtest_correlation.pypass (12 passed).Note
Low Risk
Localized utility fix; the only in-repo caller already uses the return value, so behavior there is unchanged aside from avoiding accidental input mutation.
Overview
chainladder.utils.sparse.floorno longer mutates the input COO array. It now **copy()**s first (same pattern asnan_to_numin that module), then floorsx.data, aligning withnp.floor’s copy semantics (issue #740). Type hintsCOO→COOwere added onfloor.The test that expected
result is awas renamed totest_floor_returns_copyand now checks the input values stay unchanged while the returned array is floored.Reviewed by Cursor Bugbot for commit 876211f. Bugbot is set up for automated code reviews on this repo. Configure here.